// 根据数组项字段名
export function getAttrs (arr = [], key) {
  return arr.map(item => item[key])
}

export function validNumberRangeInput (obj, minKey, maxKey) {
  if (typeof obj[minKey] === 'undefined' || typeof obj[maxKey] === 'undefined') return true
  return obj[minKey] <= obj[maxKey]
}

// 表单重置
export function resetForm (refName) {
  if (this.$refs[refName]) {
    this.$refs[refName].resetFields()
  }
}

// 添加日期范围
export function addDateRange (dateRange, beginKey, endKey) {
  let dateObj = { [beginKey]: undefined, [endKey]: undefined }
  if ((dateRange ?? false) && dateRange.length > 0) {
    dateObj[beginKey] = dateRange[0] + ' 00:00:00'
    dateObj[endKey] = dateRange[1] + ' 23:59:59'
  }
  return dateObj
}

// 删除最后一条数据 返回上一页
export function getCurrentPage ({ total, pageSize, pageIndex }) {
  const totalPage = Math.ceil((total - 1) / pageSize)
  let currentPage = pageIndex > totalPage ? totalPage : pageIndex
  currentPage = currentPage < 1 ? 1 : currentPage
  return currentPage
}

// 回显数据字典
export function selectDictLabel (datas, value) {
  var actions = []
  Object.keys(datas).map((key) => {
    if (datas[key].dictValue === value) {
      actions.push(datas[key].dictLabel)
      return false
    }
  })
  return actions.join('')
}

export function selectItemsLabel (datas, value) {
  var actions = []
  Object.keys(datas).map((key) => {
    if (datas[key].key === ('' + value)) {
      actions.push(datas[key].value)
      return false
    }
  })
  return actions.join('')
}

// 字符串格式化(%s )
export function sprintf (str) {
  var args = arguments; var flag = true; var i = 1
  str = str.replace(/%s/g, function () {
    var arg = args[i++]
    if (typeof arg === 'undefined') {
      flag = false
      return ''
    }
    return arg
  })
  return flag ? str : ''
}

// 转换字符串，undefined,null等转化为""
export function praseStrEmpty (str) {
  if (!str || str === 'undefined' || str === 'null') {
    return ''
  }
  return str
}

/**
 * 深拷贝
 */
// eslint-disable-next-line sonarjs/cognitive-complexity
export const deepCopy = source => {
  const root = Array.isArray(source) ? [] : {}
  // 栈
  const loopList = [
    {
      parent: root,
      key: undefined,
      data: source
    }
  ]
  while (loopList.length) {
    // 深度优先
    const node = loopList.pop()
    const parent = node.parent
    const key = node.key
    const data = node.data

    // 初始化赋值目标，key为undefined则拷贝到父元素，否则拷贝到子元素
    let res = parent
    if (typeof key !== 'undefined') {
      res = parent[key] = Array.isArray(data) ? [] : {}
    }

    for (let k in data) {
      if (data.hasOwnProperty(k)) {
        // eslint-disable-next-line max-depth
        if (typeof data[k] === 'object') {
          // 下一次循环
          loopList.push({
            parent: res,
            key: k,
            data: data[k]
          })
        } else {
          res[k] = data[k]
        }
      }
    }
  }

  return root
}
export function getDateByMinutes (minutes, model) {
  let self = this
  let hourMinute = 60
  let dayMinute = hourMinute * 24
  let monthMinute = dayMinute * 30// 一月默认是30天
  let yearMinute = dayMinute * 365

  if (minutes >= yearMinute) { // 年
    let year = parseInt(minutes / yearMinute)
    model.year = year
    let residue = minutes % yearMinute// 剩分钟
    if (residue > 0) {
      self.getDateByminute(residue, model)
    }
  } else if (minutes >= monthMinute) { // 月
    let month = parseInt(minutes / monthMinute)
    model.month = month
    let residue = minutes % monthMinute// 剩分钟
    if (residue > 0) {
      self.getDateByminute(residue, model)
    }
  } else if (minutes >= dayMinute) { // 日
    let day = parseInt(minutes / dayMinute)
    model.day = day
    return model
  }
}
